package ttelectronic.mobileholder.core;

import java.util.ArrayList;
import java.util.Set;

import ttelectronic.mobileholder.common.MHCommon;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;

public class MHBluetoothDevice {

	private BluetoothAdapter mBluetoothAdapter;
	private ArrayList<MHDeviceDetailObject> mListDevicesDiscovery;
	private ArrayList<MHDeviceDetailObject> mListDevicesPaired;
	private Context mContext;

	@SuppressWarnings("unused")
	private boolean isDiscovering;

	public MHBluetoothDevice(Context context, BluetoothAdapter bluetoothAdapter) {
		mBluetoothAdapter = bluetoothAdapter;
		mContext = context;
		mListDevicesDiscovery = new ArrayList<MHDeviceDetailObject>();
		mListDevicesPaired = new ArrayList<MHDeviceDetailObject>();

		doDiscovery();
	}

	// Get list bluetooth devices paired
	public ArrayList<MHDeviceDetailObject> getListDevicesPaired() {
		mListDevicesPaired.clear();
		Set<BluetoothDevice> pairedDevices = mBluetoothAdapter
				.getBondedDevices();
		if (pairedDevices.size() > 0) {
			for (BluetoothDevice device : pairedDevices) {
				mListDevicesPaired.add(new MHDeviceDetailObject(device
						.getName(), device.getAddress(), device
						.getBluetoothClass().getDeviceClass()));
			}
		}
		return mListDevicesPaired;
	}

	public ArrayList<MHDeviceDetailObject> getListDevicesDiscovery() {
		return mListDevicesDiscovery;
	}

	private void doDiscovery() {
		isDiscovering = true;
		monitorDiscovery();
		discoverDevices();
		//
		// Log.d(MHCommon.TAG, "Before - btAdapter.isDiscovering()"
		// + mBluetoothAdapter.isDiscovering());
		while (mBluetoothAdapter.isDiscovering()) {
		}
		//
		// Log.d(MHCommon.TAG, "After - btAdapter.isDiscovering()"
		// + mBluetoothAdapter.isDiscovering());
		// Log.d(MHCommon.TAG, "Returning from getDiscoveredList()");
	}

	private void monitorDiscovery() {
		mContext.registerReceiver(discoveryMonitor, new IntentFilter(
				BluetoothAdapter.ACTION_DISCOVERY_STARTED));
		mContext.registerReceiver(discoveryMonitor, new IntentFilter(
				BluetoothAdapter.ACTION_DISCOVERY_FINISHED));
	}

	BroadcastReceiver discoveryMonitor = new BroadcastReceiver() {

		String dStarted = BluetoothAdapter.ACTION_DISCOVERY_STARTED;
		String dFinished = BluetoothAdapter.ACTION_DISCOVERY_FINISHED;

		@Override
		public void onReceive(Context context, Intent intent) {
			if (dStarted.equals(intent.getAction())) {
				// Discovery has started.
				Log.d(MHCommon.TAG, "Discovery Started...");
			} else if (dFinished.equals(intent.getAction())) {
				// Log.d(MHCommon.TAG, "Discovery Finished...");
				// Log.d(MHCommon.TAG,
				// "doneDiscovering()     isDiscovering = false");
				isDiscovering = false;
				// Log.d(MHCommon.TAG,
				// "Discovery Completed...discoveredDevices Size = "
				// + mListDevicesDiscovery.size());
				// for (int i = 0; i < mListDevicesDiscovery.size(); i++) {
				// Log.d(MHCommon.TAG, "Device("
				// + i
				// + "): "
				// + mListDevicesDiscovery.get(i).getDeviceName()
				// + "  MAC = "
				// + mListDevicesDiscovery.get(i)
				// .getDeviceMacAddress());
				// }
				// Log.d(MHCommon.TAG, "Size at doneDiscovering() :"
				// + mListDevicesDiscovery.size());
			}
		}
	};

	private void discoverDevices() {
		while (!mBluetoothAdapter.isEnabled()) {
		}
		mListDevicesDiscovery.clear();
		mBluetoothAdapter.startDiscovery();
		while (!mBluetoothAdapter.isDiscovering()) {
		}
		BroadcastReceiver discoveryResult = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				BluetoothDevice remoteDevice;
				remoteDevice = intent
						.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

				MHDeviceDetailObject device = new MHDeviceDetailObject(
						remoteDevice.getName(), remoteDevice.getAddress(),
						remoteDevice.getBluetoothClass().getDeviceClass());

				if (!mListDevicesDiscovery.contains(device))
					mListDevicesDiscovery.add(device);
			}
		};
		mContext.registerReceiver(discoveryResult, new IntentFilter(
				BluetoothDevice.ACTION_FOUND));
	}

	public MHDeviceDetailObject isContain(
			ArrayList<MHDeviceDetailObject> listDevicesDiscovery,
			ArrayList<MHDeviceDetailObject> listDevicesPaired) {
		for (MHDeviceDetailObject devicePaired : listDevicesPaired) {
			for (MHDeviceDetailObject deviceDiscovery : listDevicesDiscovery) {
				if (devicePaired.getDeviceMacAddress().compareTo(
						deviceDiscovery.getDeviceMacAddress()) == 0)
					return devicePaired;
			}
		}
		return null;
	}

	public MHDeviceDetailObject isContain() {
		ArrayList<MHDeviceDetailObject> listDevicesDiscovery = getListDevicesDiscovery();
		ArrayList<MHDeviceDetailObject> listDevicesPaired = getListDevicesPaired();
		return isContain(listDevicesDiscovery, listDevicesPaired);
	}

}
